<template>
  <view>
    <!-- UniApp 会自动处理页面路由，不需要 router-view -->
  </view>
</template>

<script setup>
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import { useUserStore } from '@/store/modules/user'
import { getToken } from '@/utils/auth'

// 应用启动
onLaunch(() => {
  // 设置状态栏高度CSS变量
  setStatusBarHeight()
  // 检查登录状态并恢复用户信息
  checkAndRestoreUserState()
})

// 应用显示
onShow(() => {
  // 应用从后台恢复时，检查用户状态
  checkUserStateOnShow()
})

// 应用隐藏
onHide(() => {
  // 应用隐藏处理
})




// 检查并恢复用户登录状态
async function checkAndRestoreUserState() {
  try {
    const token = getToken()
    if (token) {
      const userStore = useUserStore()
      
      // 如果store中没有用户信息，尝试重新获取
      if (!userStore.id || !userStore.name) {
        await userStore.refreshUserInfo()
      }
    }
  } catch (error) {
    console.error('恢复用户状态失败:', error)
    const userStore = useUserStore()
    await userStore.logOut()
  }
}

// 应用显示时检查用户状态
async function checkUserStateOnShow() {
  try {
    const token = getToken()
    if (token) {
      const userStore = useUserStore()
      // 检查是否有基本用户信息，没有则重新获取
      if (!userStore.id || !userStore.name) {
        await userStore.refreshUserInfo()
      }
    }
  } catch (error) {
    console.error('应用显示时检查用户状态失败:', error)
  }
}

// 设置状态栏高度
function setStatusBarHeight() {
  // 获取系统信息
  const systemInfo = uni.getSystemInfoSync()
  const statusBarHeight = systemInfo.statusBarHeight || 0
  
  // 设置CSS变量
  if (typeof document !== 'undefined') {
    document.documentElement.style.setProperty('--status-bar-height', statusBarHeight + 'px')
  }
}
</script>

<style lang="scss">
// 导入设计系统样式
@import '@/styles/theme.scss';
@import '@/styles/common.scss';

// 状态栏高度变量（动态设置）
:root {
  --status-bar-height: 0px;
}

// 全局样式重置（微信小程序兼容）
page, view, text, button, input, textarea, scroll-view, swiper, swiper-item, navigator, image, video, map, canvas, open-data, web-view, ad, official-account, live-player, live-pusher, cover-view, cover-image, icon, text, rich-text, progress, checkbox, form, label, radio, slider, switch, picker, picker-view, movable-area, movable-view, contact-button {
  box-sizing: border-box;
}

page {
  background-color: $gray-100;
  color: $gray-800;
  font-size: $text-base;
  line-height: 1.5;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

// 全局滚动条样式
::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba($gray-400, 0.5);
  border-radius: $radius-full;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba($gray-400, 0.8);
}

// 全局动画
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
}

.slide-up-enter-active, .slide-up-leave-active {
  transition: all 0.3s ease;
}

.slide-up-enter-from {
  transform: translateY(100%);
  opacity: 0;
}

.slide-up-leave-to {
  transform: translateY(-100%);
  opacity: 0;
}

// uni-app 组件样式调整
.uni-searchbar {
  background-color: transparent !important;
}

.uni-searchbar__box {
  background-color: $gray-100 !important;
  border-radius: $radius-lg !important;
  border: 1px solid $border-color !important;
}

.uni-card {
  box-shadow: $shadow-sm !important;
  border-radius: $radius-lg !important;
  border: 1px solid $border-color !important;
}

.uni-forms-item__label {
  color: $gray-700 !important;
  font-weight: $font-medium !important;
}

.uni-easyinput__content-input {
  border-radius: $radius-md !important;
  border: 1px solid $border-color !important;
  
  &:focus {
    border-color: $primary !important;
  }
}

.uni-data-select {
  border-radius: $radius-md !important;
  border: 1px solid $border-color !important;
}

.uni-datetime-picker {
  border-radius: $radius-md !important;
  border: 1px solid $border-color !important;
}

.uni-tag {
  border-radius: $radius-full !important;
}

.uni-load-more {
  margin: $space-lg 0 !important;
}

.uni-nav-bar {
  box-shadow: $shadow-sm !important;
}

.uni-popup {
  border-radius: $radius-lg !important;
  overflow: hidden !important;
}

// 安全区域适配
.safe-area-inset-bottom {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

.safe-area-inset-top {
  padding-top: constant(safe-area-inset-top);
  padding-top: env(safe-area-inset-top);
}

// 平台特定样式
/* #ifdef H5 */
.h5-only {
  display: block;
}
/* #endif */

/* #ifdef MP-WEIXIN */
.mp-only {
  display: block;
}
/* #endif */

/* #ifdef APP-PLUS */
.app-only {
  display: block;
}
/* #endif */

// 隐藏非当前平台元素
/* #ifndef H5 */
.h5-only {
  display: none !important;
}
/* #endif */

/* #ifndef MP-WEIXIN */
.mp-only {
  display: none !important;
}
/* #endif */

/* #ifndef APP-PLUS */
.app-only {
  display: none !important;
}
/* #endif */

// 响应式工具类
@media (max-width: 750rpx) {
  .mobile-hidden {
    display: none !important;
  }
}

@media (min-width: 750rpx) {
  .mobile-only {
    display: none !important;
  }
}

// 打印样式
@media print {
  .no-print {
    display: none !important;
  }
  
  .print-only {
    display: block !important;
  }
}

// 深色模式支持（预留）
@media (prefers-color-scheme: dark) {
  // 深色模式样式将在后续版本中添加
}

// 高对比度模式支持
@media (prefers-contrast: high) {
  .btn {
    border-width: 2px !important;
  }
  
  .card {
    border-width: 2px !important;
  }
}

// 减少动画模式支持
@media (prefers-reduced-motion: reduce) {
  page, view, text, button, input, textarea, scroll-view, swiper, swiper-item, navigator, image, video, map, canvas, open-data, web-view, ad, official-account, live-player, live-pusher, cover-view, cover-image, icon, text, rich-text, progress, checkbox, form, label, radio, slider, switch, picker, picker-view, movable-area, movable-view, contact-button {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

// 全局禁用选择（某些情况下需要）
.no-select {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

// 全局启用选择
.allow-select {
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  user-select: text;
}

// 全局光标样式
.cursor-pointer {
  cursor: pointer;
}

.cursor-not-allowed {
  cursor: not-allowed;
}

.cursor-wait {
  cursor: wait;
}

// 全局文本处理
.text-break {
  word-break: break-word;
  word-wrap: break-word;
  hyphens: auto;
}

.text-no-wrap {
  white-space: nowrap;
}

.text-pre {
  white-space: pre;
}

.text-pre-wrap {
  white-space: pre-wrap;
}

.text-pre-line {
  white-space: pre-line;
}
</style>